/**
 * 神州信息(2012)
 */
package net.abbp.report.export;

import java.io.IOException;

import net.abbp.core.util.StringUtils;
import net.abbp.report.model.Cell;
import net.abbp.report.model.ReportModel;
import net.abbp.report.model.cell.style.HorizentalAlignment;
import net.abbp.report.model.cell.style.Style;
import net.abbp.report.model.cell.style.VerticalAlignment;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;

/**
 * 默认值转换器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Jul 31, 2012 lexloo
 * </pre>
 */
public class DefaultValueExport implements IValueExport {
    @Override
    public PdfPCell cell2PdfCell(Cell cell, ReportModel model) {
        PdfPCell rtn = new PdfPCell();
        Object value = cell.getValue();
        Paragraph para=null;
        if (value != null) {
            try {
                BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
                para = new Paragraph(StringUtils.convertToString(value), FontChinese);
                rtn.addElement(para);
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        rtn.setFixedHeight(model.getRowHeight(cell.getRow()));
        Style style = cell.getStyle();

        rtn.setUseDescender(true);
        rtn.setUseAscender(true);
        VerticalAlignment vAlign = style.getVerticalAlignment();
        if (vAlign == VerticalAlignment.Top) {
            rtn.setVerticalAlignment(Element.ALIGN_TOP);
        } else if (vAlign == VerticalAlignment.Bottom) {
            rtn.setVerticalAlignment(Element.ALIGN_BOTTOM);
        } else {
            rtn.setVerticalAlignment(Element.ALIGN_MIDDLE);
        }

        if (para != null) {
            HorizentalAlignment hAlign = style.getHorizentalAlignment();
            if (hAlign == HorizentalAlignment.Left) {
                para.setAlignment(Element.ALIGN_LEFT);
                rtn.setHorizontalAlignment(Element.ALIGN_LEFT);
            } else if (hAlign == HorizentalAlignment.Right) {
                para.setAlignment(Element.ALIGN_RIGHT);
                rtn.setHorizontalAlignment(Element.ALIGN_RIGHT);
            } else {
                para.setAlignment(Element.ALIGN_CENTER);
                rtn.setHorizontalAlignment(Element.ALIGN_CENTER);
            }
        }
        return rtn;
    }
}
